package com.example.myapplication

import android.Manifest
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.PermissionUtils
import com.example.myapplication.adapter.DeviceAdapter
import com.wakeup.sdk.ble.WeikeBleMgr
import com.wakeup.sdk.ble.ext.EventType
import com.wakeup.sdk.ble.ext.OnEventListener
import com.wakeup.sdk.ble.ext.connect.DeviceState
import com.wakeup.sdk.ble.ext.connect.ScanDevice
import com.wakeup.sdk.ble.utils.BleUtil

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"
    private val mAdapter = DeviceAdapter()

    private val callback = object : OnEventListener {
        override fun onEvent(type: EventType, code: Int, data: Any?) {
            if (type == EventType.TYPE_DEVICE_SCAN) {
                if (code == 0) {
                    mAdapter.addData(data as ScanDevice)
                }
            } else if (type == EventType.TYPE_DEVICE_STATE) {
                Log.d(TAG, "device state code = $code ; data = $data")
                if (code == DeviceState.STATE_CONNECTED) {
                    startActivity(Intent(this@MainActivity, DeviceActivity::class.java))
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        WeikeBleMgr.init(this)
        WeikeBleMgr.openLog(true)
        WeikeBleMgr.setConnectUser("996皮炎平11")
        WeikeBleMgr.registerListener(callback, EventType.TYPE_DEVICE_STATE)
        WeikeBleMgr.registerListener(callback, EventType.TYPE_DEVICE_SCAN)

        val btn = findViewById<Button>(R.id.btn_start)
        btn.setOnClickListener { test() }

        val rv = findViewById<RecyclerView>(R.id.rv_device)
        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = mAdapter
    }

    private fun test() {
        mAdapter.setNewData(mutableListOf())
        PermissionUtils.permission(*getBlePermissions())
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    WeikeBleMgr.scanDevice(listOf("XPAW014"))
                }

                override fun onDenied() {
                }
            })
            .request()
    }

    private fun getBlePermissions(): Array<String> {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            arrayOf(
                Manifest.permission.BLUETOOTH_SCAN,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.BLUETOOTH_CONNECT
            )
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION
        ) else {
            arrayOf(
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
            )
        }
    }
}
